﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMSGSC.BLL;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using PMSGSC.DAL;

namespace PMSGSC.App
{
    public partial class frmUpdateCounter : DevExpress.XtraEditors.XtraForm
    {
        public frmUpdateCounter(List<Equipment> equipments)
        {
            InitializeComponent();
            equipmentBindingSource.DataSource = equipments;
        }
        // Tạo một đối tượng lưu trữ các thông tin trên View ==> truyền xuống các layer bên dưới
        private Equipment equipment = new Equipment();

        List<Equipment> listEquipment = new List<Equipment>();
        //Hiển thị số thứ tự của gridview
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void AddEquipIntoTempList(Equipment eq)
        {
            foreach (Equipment equip in listEquipment)
            {
                if (equip.Equipment_ID == eq.Equipment_ID)
                {
                    listEquipment.Remove(equip);
                    break;
                }
            }
            listEquipment.Add(eq);
        }

        // Sự kiện cập nhật dữ liệu xuống databse
        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            equipment = equipmentBindingSource.Current as Equipment;
            AddEquipIntoTempList(equipment);
        }

        private void SaveDataToDatabase()
        {
            if (listEquipment.Count > 0)
            {
                if (XtraMessageBox.Show("Do you want to save all changes", "Infomation", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    foreach (Equipment equip in listEquipment)
                    {
                        HistoryCounter counterHistory = new HistoryCounter();
                        counterHistory.Counters = equip.CurrentCounter;
                        counterHistory.DateUpdate = equip.TempDate;
                        counterHistory.Equipment_ID = equip.Equipment_ID;
                        bool historyResult = CounterHistory_BLL.Insert(counterHistory);

                        int ValueChange = equip.CurrentCounter.Value - equip.OldCounter.Value;
                        WorkOrder_BLL.UpdateCounterOfJob(equip.Equipment_ID, ValueChange);

                        equip.OldCounter = equip.CurrentCounter;
                        equip.IsCounter = true;
                        bool result = Equipment_BLL.UpdateCounter(equip);
                    }
                    listEquipment.Clear();
                    XtraMessageBox.Show("Save all Successfull !", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    equipmentBindingSource.DataSource = Equipment_BLL.GetAll(frmMain.ship.Ship_ID);
                }
            }
        }

        // Sự kiện click vào thành Navigator
        private void datnaviMain_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.EndEdit:
                    SaveDataToDatabase();
                    break;
                case NavigatorButtonType.CancelEdit:

                    break;
            }
        }

        private static void OnlyInputNumberic(KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        private void txtSetvalue_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnlyInputNumberic(e);
        }

        private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Name == "colCurrentCounter")
            {
                GridView view = sender as GridView;
                GridColumn dateCol = view.Columns[4];
                view.SetFocusedRowCellValue(dateCol, DateTime.Today.ToString());
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDataToDatabase();
        }
    }
}